package test;

import test.bean.UserBean;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class LoginFrame extends JFrame {

    private JFrame frame;
    private JTextField usernameField;
    private JPasswordField pwdField;
    private JButton registerBtn;
    private JButton loginBtn;


    public JFrame getFrame() {
        return frame;
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                LoginFrame window = new LoginFrame();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * Create the application.
     */
    public LoginFrame() {
        initialize();
    }

    public LoginFrame(String username, String password) {
        this();
        this.usernameField.setText(username);
        this.pwdField.setText(password);
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        frame = new JFrame();
        frame.setBounds(100, 100, 540, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);


        JLabel lblNewLabel = new JLabel("欢迎来到SHOP！");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 36));
        lblNewLabel.setBounds(104, 24, 281, 66);
        frame.add(lblNewLabel);

        JLabel numLbl = new JLabel("");
        numLbl.setBounds(98, 132, 58, 15);
        frame.getContentPane().add(numLbl);

        usernameField = new JTextField();
        usernameField.setBounds(183, 109, 151, 31);
        frame.getContentPane().add(usernameField);
        usernameField.setColumns(10);

        JLabel pwdLbl = new JLabel("");
        pwdLbl.setBounds(98, 170, 58, 15);
        frame.getContentPane().add(pwdLbl);

        pwdField = new JPasswordField();
        pwdField.setBounds(183, 182, 151, 31);
        frame.getContentPane().add(pwdField);

        loginBtn = new JButton("登录");
        loginBtn.setBounds(200, 252, 97, 38);
        loginBtn.setContentAreaFilled(false);//设置透明
        loginBtn.setFont(new java.awt.Font("华文彩云", 1, 25));//设置字体
        loginBtn.setBorderPainted(false);//设置无边框
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            UserBean userBean = new UserBean();
                            userBean.setUsername(usernameField.getText());
                            userBean.setPassword(pwdField.getText());
                            Store window = new Store(userBean);
                            window.frame.setVisible(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
        frame.getContentPane().add(loginBtn);

        registerBtn = new JButton("注册");
        registerBtn.setBounds(98, 259, 97, 23);
        registerBtn.setContentAreaFilled(false);//设置透明
        registerBtn.setFont(new java.awt.Font("华文彩云", 1, 25));//设置字体
        registerBtn.setBorderPainted(false);//设置无边框
        frame.getContentPane().add(registerBtn);
        registerBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                frame.dispose();
                JFrame newFrame = new RegisterFrame();
                newFrame.setVisible(true);
            }
        });


        ImageIcon icon = new ImageIcon("test/image/04.jpg");
        JLabel lblBackground = new JLabel(); // 创建一个标签组件对象
        lblBackground.setIcon(icon); // 设置标签组件要显示的图标
        lblBackground.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); // 设置组件的显示位置及大小
        this.frame.getContentPane().add(lblBackground);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

}

